package com.sync.utils.framework.redis.service;

import com.sync.utils.framework.redis.entity.Message;
import com.sync.utils.framework.redis.utils.MessageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.CountDownLatch;


/**
 * 注入消息接收类
 *
 * @Author YWF
 * @Date 2022/1/24 10:28
 */

@Service
public class ReceiverRedisMessage {

    private final static Logger logger = LoggerFactory.getLogger(ReceiverRedisMessage.class);

    private CountDownLatch latch;

    @Autowired
    public ReceiverRedisMessage(CountDownLatch latch){
        this.latch = latch;
    }

    @Autowired
    private MessageUtil messageUtil;


    public void receiveMessage(String messageObj){

        logger.info("[开始消费REDIS消息队列public_topic数据...]");
        try {
            Message message = messageUtil.MsgCastObject(messageObj,Message.class);
            logger.info("消息对象：{}",message);
        } catch (Exception e) {
            logger.error("[消费REDIS消息队列phone数据失败，失败信息:{}]", e.getMessage());
            e.printStackTrace();
        }
        latch.countDown();
    }

    /**
     * 队列消息接收方法
     *
     * @param jsonMsg
     */
    public void receiveMessage2(String jsonMsg) {
        logger.info("[开始消费REDIS消息队列phoneTest2数据...]");
        try {
            System.out.println(jsonMsg);
            /**
             *  此处执行自己代码逻辑 例如 插入 删除操作数据库等
             */

            logger.info("[消费REDIS消息队列phoneTest2数据成功.]");
        } catch (Exception e) {
            logger.error("[消费REDIS消息队列phoneTest2数据失败，失败信息:{}]", e.getMessage());
        }
        latch.countDown();
    }

}
